#########################
### Dyad-level coding ###
#########################


# Merge QOG for both sides
ddtrade <- merge(x = ddtrade, y = factors, by.x = c("ccode1", "year"), by.y = c("ccodecow", "year"))
ddtrade <- rename(ddtrade, c("gle_cgdpc" = "gle_cgdpc1", "wdi_landara" = "wdi_landara1", "gle_pop" = "gle_pop1", "gle_gdp" = "gle_gdp1",
                             "p_polity2" = "polity1"))

ddtrade <- merge(x = ddtrade, y = factors, by.x = c("ccode2", "year"), by.y = c("ccodecow", "year"))
ddtrade <- rename(ddtrade, c("gle_cgdpc" = "gle_cgdpc2", "wdi_landara" = "wdi_landara2", "gle_pop" = "gle_pop2", "gle_gdp" = "gle_gdp2",
                             "p_polity2" = "polity2"))

# Merge in constant dollar multiplier
ddtrade <- merge(x = ddtrade, y = constant, by.x = c("year"), by.y = c("year"))


# Generate logged exports 1 -> 2 variable
ddtrade$log.exp1to2 <- log((ddtrade$flow2/ddtrade$constant) + 1)

# GDP 1 and 2
ddtrade$log.gdp1 <- log(ddtrade$gle_gdp1)
ddtrade$log.gdp2 <- log(ddtrade$gle_gdp2)

# merge distance
ddtrade <- merge(x = ddtrade, y = cepii, by.x = c("ccode1", "ccode2"), by.y = c("ccode1", "ccode2"))
ddtrade$log.distance <- log(ddtrade$distcap)

# Group (dyad) var
ddtrade$dyadid <- (ddtrade$ccode1 * 1000) + ddtrade$ccode2

# Order data
ddtrade <- ddtrade[order(ddtrade$dyadid, ddtrade$year),]

# Lead DV (log trade value)
ddtrade <- slide(ddtrade, Var = "log.exp1to2", GroupVar = "dyadid", slideBy = 1)


## Hegemon data to merge at dyad level ##

# Subset useful variables
heg.dyad <- subset(final.data, select = c("ccode1", "year", "lemkeregion", "cinc", "regshare", "totcinc", "count", "NHHI", "hegregshare", "hegpolity", "hegemon"))

# Merge for dyadic state 1
ddtrade <-  merge(x = ddtrade, y = heg.dyad, by.x = c("ccode1", "year"), by.y = c("ccode1", "year"))
ddtrade <- rename(ddtrade, c("lemkeregion" = "lemkeregion1", "cinc" = "cinc1", "regshare" = "regshare1", "totcinc" = "totcinc1",
                             "count" = "count1", "NHHI" = "NHHI1", "hegregshare" = "hegregshare1", "hegpolity" ="hegpolity1",
                             "hegemon" = "hegemon1"))

# Merge for dyadic state 2
ddtrade <-  merge(x = ddtrade, y = heg.dyad, by.x = c("ccode2", "year"), by.y = c("ccode1", "year"))
ddtrade <- rename(ddtrade, c("lemkeregion" = "lemkeregion2", "cinc" = "cinc2", "regshare" = "regshare2", "totcinc" = "totcinc2",
                             "count" = "count2", "NHHI" = "NHHI2", "hegregshare" = "hegregshare2", "hegpolity" ="hegpolity2",
                             "hegemon" = "hegemon2"))

# Create factor endowment variables - log of exporter/importer for land (arable), labor (pop), capital (gdppc)
ddtrade$log.arabrat <- log(ddtrade$wdi_landara1 / ddtrade$wdi_landara2)
ddtrade$log.poprat <- log(ddtrade$gle_pop1 / ddtrade$gle_pop2)
ddtrade$log.caprat <- log(ddtrade$gle_cgdpc1 / ddtrade$gle_cgdpc2)

# Manually create factors for two-way fe
ddtrade$fyear <- as.factor(ddtrade$year)
ddtrade$fdyad <- as.factor(ddtrade$dyadid)


## Data subsets - note: we only look at non-hegemon dyads given the large N that slows down R's processing ##
# Subset data for same-region dyads
sameregion.nh <- subset(ddtrade, select = c("ccode1", "ccode2", "log.exp1to21", "NHHI1", "hegpolity1", "totcinc1", "count1", "log.gdp1", "log.gdp2", 
                                            "log.distance", "log.arabrat", "log.poprat", "log.caprat", "fyear", "fdyad",
                                            "lemkeregion1"), lemkeregion1 == lemkeregion2 & hegemon1 == 0 & hegemon2 == 0)

# Subset data for different-region dyads
difregion.nh <- subset(ddtrade, select = c("ccode1", "ccode2", "log.exp1to21", "NHHI1", "hegpolity1", "totcinc1", "count1", "log.gdp1", "log.gdp2",
                                           "NHHI2", "hegpolity2", "totcinc2", "count2",
                                           "log.distance", "log.arabrat", "log.poprat", "log.caprat", "fyear", "fdyad",
                                           "lemkeregion1", "lemkeregion2"), lemkeregion1 != lemkeregion2 & hegemon1 == 0 & hegemon2 == 0)
